package cn.sumpu.app.guoguojie.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.sumpu.app.guoguojie.biz.CmsChannelBiz;
import cn.sumpu.app.guoguojie.biz.CmsContentBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.dao.CmsChannelDao;
import cn.sumpu.app.guoguojie.domain.CmsChannel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

public class CmsChannelBizImpl implements CmsChannelBiz {

	private CmsChannelDao cmsChannelDao;
	@Autowired
	private CmsContentBiz cmsContentBiz;
	@Resource
	private IdentifierBiz identifierBiz;

	public void setCmsChannelDao(CmsChannelDao cmsChannelDao) {
		this.cmsChannelDao = cmsChannelDao;
	}

	// 添加频道
	@Override
	public void addCmsChannel(CmsChannel cmsChannel)
			throws AppBusinessException {
		cmsChannel.setIdentifier(identifierBiz.generateId("cms_channel"));
		
		if("0".equals(cmsChannel.getParent())){
		    cmsChannel.setDeep(1);
		}else{
		    cmsChannel.setDeep(getChannelById(cmsChannel.getParent()).getDeep()+1);
		}
		if(Common.emptyStr(cmsChannel.getMark())){
		    cmsChannel.setMark(String.valueOf(UUID.randomUUID()));
		}
		cmsChannelDao.addCmsChannel(cmsChannel);
	}

	// 取得频道列表
	@Override
	public List<CmsChannel> getChannelList(String parent)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parent", parent);
		return cmsChannelDao.getChannelList(map);
	}
	//取得子节点的全部ID
	private List<CmsChannel> getChildrenChannelList(String parentId)
			throws AppBusinessException {
		List<CmsChannel> list = getChannelList(parentId);
		for (CmsChannel c : list) {
			c.setChildren(getChildrenChannelList(c.getIdentifier()));
		}
		return list;
	}
	private List<CmsChannel> buildChildren(String parentId)
			throws AppBusinessException {
		return getChildrenChannelList(parentId);
	}
	private String getChildrenCategoryId(String parentId)
			throws AppBusinessException {
		List<String> categoryBuilder = new ArrayList<String>();
		return getCategory(parentId, categoryBuilder);
	}
	private String getCategory(String id, List<String> list)
			throws AppBusinessException {
		List<CmsChannel> cList = buildChildren(id);
		for (CmsChannel tmp : cList) {
			list.add("'" + tmp.getIdentifier() + "'");
			List<CmsChannel> children = tmp.getChildren();
			if (null != children && children.size() > 0) {
				getCategory(tmp.getIdentifier(), list);
			}
		}
		StringBuilder s = new StringBuilder();
		for (String tmp : list) {
			s.append(tmp + ",");
		}
		s.append("'" + id + "'");
		return s.toString();
	}

	// 删除频道
	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void deleteChannel(String identifier) throws AppBusinessException {
		//取得全部子节点（包括自己）
		String identifierList = getChildrenCategoryId(identifier); 
		identifierList = "("+identifierList+")";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", identifierList);
		cmsChannelDao.deleteChannel(map);
		//删除该频道下(及子频道)的内容 	
		String contentIdList = Common.parseListToString(cmsContentBiz.getContentIdList(identifierList));
		cmsContentBiz.deleteContentByIdList(contentIdList);
	}
	//根据频道Id取得频道信息
	@Override
	public CmsChannel getChannelById(String channelId)
			throws AppBusinessException {
		return cmsChannelDao.getChannelById(channelId);
	}
	//根据频道发布内容
	@Override
	public void postContentByChannelId(String channelId)
			throws AppBusinessException {
		//取得全部子节点（包括自己）
		String identifierList = getChildrenCategoryId(channelId); 
		cmsContentBiz.updateContentAll("("+identifierList+")");
	}
	/**
     * author BruceZhang
     * @param map   parent 父ID
     * @return  根据父ID取得子节点的树结构
     * @throws AppBusinessException
     */
    @Override
    public List<CmsChannel> getChildById(String parent) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parent", parent);
        return cmsChannelDao.getChildById(map);
    }
    //更新频道
    @Override
    public void updateChannelById(CmsChannel cmsChannel) throws AppBusinessException {
        cmsChannelDao.updateChannelById(cmsChannel);
    }

}
